/*
 * @Description: 
 * @Author: paddies
 * @Date: 2024-06-03 01:30:35
 * @LastEditors: paddies
 * @LastEditTime: 2024-12-05 12:52:23
 * @FilePath: /shared_components/lib/init.dart
 */
library shared_components;

import 'package:flutter/material.dart';
import 'package:shared_components/utils/global_prefs.dart';

///实例
late final SharedManager sharedManager;

// class Singleton {
//   static Singleton? _instance;

//   Singleton._internal();

//   ///用get直接获取实例
//   static get instance {
//     if (_instance == null) {
//       _instance = Singleton._internal();
//     }

//     return _instance;
//   }

//   Singleton() {}
// }

///单例类
class SharedManager {
  // 工厂方法构造函数 - 通过UserModel()获取对象1
  factory SharedManager() => _getInstance();

  // instance的getter方法 - 通过UserModel.instance获取对象2
  static SharedManager get instance => _getInstance();

  // 静态变量_instance，存储唯一对象
  static SharedManager? _instance;

  // 获取唯一对象
  static SharedManager _getInstance() {
    _instance ??= SharedManager._internal();
    return _instance!;
  }

  //私有变量
  String token = "";
  String uploadURL = "";
  //连接超时时间
  int connectTimeout = 15000;
  //发送超时时间
  int sendTimeout = 60000;
  //请求响应成功code
  int successCode = 200;
  //重连超时
  int receiveTimeout = 60000;
  Color themeColor = const Color(0xff0000f5);
  //私有方法
  void setUploadURL(String url) {
    uploadURL = url;
  }

  void setToken(String tk) {
    token = tk;
  }

  //初始化...
  SharedManager._internal() {
    //初始化其他操作...
    // ...
  }
}

initSharedComponents(
    {String uploadURL = '',
    int connectTimeout = 15000,
    int sendTimeout = 60000,
    int successCode = 200,
    Color themeColor = const Color(0xff0000f5),
    int receiveTimeout = 60000}) async {
  print("初始化共享组件");
  sharedManager = SharedManager();
  sharedManager.connectTimeout = connectTimeout;
  sharedManager.sendTimeout = sendTimeout;
  sharedManager.successCode = successCode;
  sharedManager.themeColor = themeColor;
  sharedManager.receiveTimeout = receiveTimeout;
  sharedManager.setUploadURL(uploadURL);
  String value = await GlobalPrefs.init();
  if (value == 'ok') {
    GlobalPrefs.saveValue("hasWindow", "NO");
  }
}
